<!---load variables with default settings--->
<cfparam name="c" default=""> <!---component--->
<cfparam name="a" default=""> <!---component action--->
<cfparam name="l" default="1"> <!---layout--->
<cfparam name="id" default="">
<cfparam name="file" default="">
<cfparam name="language" default="english">
<cfset request.layout = request.site_defaultlayout>
<cfset request.layoutinclude = "">
<cfset request.language = language>
<!---set to determine initial layout of site depending on component being loaded--->
<cfif cgi.SCRIPT_NAME IS '/index.cfm'>
	<cfif len(trim(c)) IS 0 AND request.site_startup IS 1>
		<cfset c = request.site_component>
	</cfif>    
</cfif>
<!---load language files if any, first the default language file then component language files--->
<cfif isdefined('url.language') OR isdefined('form.language')><cfcookie name="admin_language" value="#language#" expires="never"></cfif>
<cfif isdefined('cookie.admin_language')><cfset request.language = cookie.admin_language></cfif>
<!---if the base language pack doesn't exist then default to the english one if it is present--->
<cfif fileexists('#request.basepath#lang/#request.language#.cfm')>
    <cfinclude template="#request.absolutepath#lang/#request.language#.cfm">
<cfelse>
	<cfif fileexists('#request.adminpath#lang/english.cfm')>
	    <cfinclude template="#request.absolutepath#lang/english.cfm">	
	</cfif>
</cfif>
<!---load component language packs if any exist--->
<cfloop query="qcs">
	<cfif fileexists('#request.basepath#lang/#qcs.name#/#request.language#.cfm')>
		<cfinclude template="#request.absolutepath#lang/#qcs.name#/#request.language#.cfm">
    <cfelse>
    	<cfif fileexists('#request.basepath#lang/#qcs.name#/english.cfm')>
	    	<cfinclude template="#request.absolutepath#lang/#qcs.name#/english.cfm">	
		</cfif>
    </cfif>
</cfloop>
<!---load component language packs if any exist--->
<cfloop query="qcs">
	<cfif fileexists('#request.basepath#components/#qcs.name#/lang/#request.language#.cfm')>
		<cfinclude template="#request.absolutepath#components/#qcs.name#/lang/#request.language#.cfm">
    <cfelse>
    	<cfif fileexists('#request.basepath#components/#qcs.name#/lang/english.cfm')>
	    	<cfinclude template="#request.absolutepath#components/#qcs.name#/lang/english.cfm">	
		</cfif>
    </cfif>
</cfloop>
<!---If a component specific layout exists, this will load that layout instead of the default layout.  Layout must contain header.cfm and footer.cfm--->
<cfif len(trim(c)) GT 0>
	<cfif fileexists('#request.basepath#themes/#request.site_theme#/layouts/#c#/header.cfm') AND fileexists('#request.basepath#themes/#request.site_theme#/layouts/#c#/footer.cfm')>
        <cfset request.layout = c>
	<cfelse>
        <cfinvoke component="cfcs.common.layout" method="readlayoutmapping" lname="#c#" returnvariable="qlayout" />
        <cfif qlayout.recordcount GT 0> 
        	<cfset request.layout = qlayout.mapto>
        </cfif>
    </cfif>
    <!---if an action is called and a specific layout is needed for a particular action then set it to that one--->
	<cfif len(trim(a)) GT 0>
		<cfif fileexists('#request.basepath#themes/#request.site_theme#/layouts/#c#_#a#/header.cfm') AND fileexists('#request.basepath#themes/#request.site_theme#/layouts/#c#_#a#/footer.cfm')>
    		<cfset request.layout = '#c#_#a#'>
		<cfelse>
            <cfinvoke component="cfcs.common.layout" method="readlayoutmapping" lname="#c#_#a#" returnvariable="qlayout" />
            <cfif qlayout.recordcount GT 0> 
                <cfset request.layout = qlayout.mapto>
            </cfif>
        </cfif>
    </cfif>
    
<!---otherwise a page is being called so check and see if a specific layout is need for a particular page--->
<cfelse>
	<cfset pagename = listLast(cgi.script_name, "/")>
	<cfif NOT pagename CONTAINS 'index.cfm'>
	    <cfset pagename = replacenocase(pagename, ".cfm", "", "ALL")>
	<cfelse>
    	<cfif request.site_startup IS 0 AND len(trim(c)) IS 0> <!---if the default page is being called then set it to load the correct layout--->
			<cfset pagename = request.site_startpage>
            <cfset pagename = replacenocase(pagename, ".cfm", "", "ALL")>        
		</cfif>
	</cfif>
	<!---check if root foler in request is mapped--->
    <cfif listlen(cgi.SCRIPT_NAME, "/") GT 0>
        <cfset foldername = listgetat(cgi.SCRIPT_NAME, 1, "/")>
        <cfset foldername = "/#foldername#/">
        <cfinvoke component="cfcs.common.layout" method="readlayoutmapping" lname="#foldername#" returnvariable="qlayout" />
        <cfif qlayout.recordcount GT 0> 
            <cfset request.layout = qlayout.mapto>
        </cfif>
    </cfif>
    <!---below will override if specific page is mapped--->
	<cfif fileexists('#request.basepath#themes/#request.site_theme#/layouts/#pagename#/header.cfm') AND fileexists('#request.basepath#themes/#request.site_theme#/layouts/#pagename#/footer.cfm')>
        <cfset request.layout = pagename>
	<cfelse>
        <cfinvoke component="cfcs.common.layout" method="readlayoutmapping" lname="#pagename#" returnvariable="qlayout" />
        <cfif qlayout.recordcount GT 0> 
            <cfset request.layout = qlayout.mapto>
        </cfif>
    </cfif>
</cfif>	
<!---if there is an included layout for the component then set the variable to load it into the layout in loadstyles.cfm--->
<cfif fileexists('#request.basepath#themes/#request.site_theme#/layouts/#request.layout#/include_header.cfm') 
	AND fileexists('#request.basepath#themes/#request.site_theme#/layouts/#request.layout#/include_footer.cfm')>
	<cfset request.layoutinclude = request.layout>
</cfif>
<!---if the index.cfm file is being called and no component is defined, and the default site action is to load a component, then load the default component--->
<cfif cgi.SCRIPT_NAME IS '/index.cfm'>
	<cfif len(trim(c)) IS 0>
		<cfif request.site_startup IS 1>
            <cfset c = request.site_component>
        </cfif>
	</cfif>
</cfif>

<cfif isdefined('url.layout')>
	<cfset request.layout = url.layout>
</cfif>